﻿using HospitalFollowUpSystem.DBC;
using HospitalFollowUpSystem.Entity;
using MetroFramework.Forms;
using System.Windows.Forms;

namespace HospitalFollowUpSystem.View
{
    public partial class FrmAdmin : MetroForm
    {
        public FrmAdmin()
        {
            InitializeComponent();
            cbPower.SelectedIndex = 0;
        }

        private void btnAddOpera_Click(object sender, System.EventArgs e)
        {
            var errStr = "";
            //
            var username = txtUsername.Text.Trim();
            HRCM_DB db = new HRCM_DB();
            var operaNum = db.ReadOperatorNum(username);
            //
            var name = txtName.Text.Trim();
            //
            if (operaNum != 0)
            {
                errStr += "--此账号已被占用--";
            }
            //
            if (name.Equals(""))
            {
                errStr += "--姓名必须填写--";
            }
            //数据格式无错
            if (errStr.Equals(""))
            {
                Operator opera = new Operator();
                opera.Name = name;
                opera.Username = username;
                opera.Password = "0";
                opera.Power = cbPower.Text.Trim();

                if (db.InsertOperator(opera))
                {
                    labStatus.Text = "--增加成功--";
                }
                else
                {
                    labStatus.Text = "--增加失败 联系管理员--";
                }
            }
            else
            {
                labStatus.Text = errStr;
            }

            db.Database.Close();
        }

        private void btnReadOpera_Click(object sender, System.EventArgs e)
        {
            WashFace();
            var errStr = "";
            HRCM_DB db = new HRCM_DB();
            Operator opera = null;
            //
            var username = txtUsername2.Text.Trim();
            if (username.Equals(""))
            {
                errStr = "--输入账号--";
            }
            else
            {
                opera = db.ReadOperator(username);
                if (opera is null)
                {
                    errStr = "--未找到此操作员--";
                }
                else
                {
                    labUsername.Text = opera.Username;
                    labName.Text = opera.Name;
                    labIsStop.Visible = opera.IsStop;
                }
            }

            labStatus2.Text = errStr;
            db.Database.Close();
        }

        private void btnReturnToZero_Click(object sender, System.EventArgs e)
        {
            var username = labUsername.Text.Trim();
            if (username.Equals(""))
            {
                labStatus2.Text = "--先查询账号--";
            }
            else
            {
                HRCM_DB db = new HRCM_DB();

                db.UpDateOperaPWD(username, "0");
                labStatus2.Text = "密码重置成功";

                db.Database.Close();
            }
        }

        private void btnStop_Click(object sender, System.EventArgs e)
        {
            var username = labUsername.Text.Trim();
            if (username.Equals(""))
            {
                labStatus2.Text = "--先查询账号--";
            }
            else
            {
                HRCM_DB db = new HRCM_DB();

                db.UpDateOperaIsStop(username);
                labStatus2.Text = "账号停用成功";

                db.Database.Close();
            }
        }

        private void WashFace()
        {
            labName.Text = "";
            labUsername.Text = "";
            labIsStop.Visible = false;
        }
    }
}
